package com.wl.java_spring_boot01.modules.test.service.impl;

import com.wl.java_spring_boot01.modules.common.vo.Result;
import com.wl.java_spring_boot01.modules.common.vo.SearchVo;
import com.wl.java_spring_boot01.modules.test.entity.Student;
import com.wl.java_spring_boot01.modules.test.repository.StudentRepository;
import com.wl.java_spring_boot01.modules.test.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author 汪林
 * @date 2020/8/13 8:38
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentRepository studentRepository;

    //增加
    @Override
    public Result<Student> insertStudent(Student student) {
        studentRepository.saveAndFlush(student);
        return new Result<Student>(
                Result.ResultStatus.SUCCESS.status,"insert success",student
        );
    }

    //id查找
    @Override
    public Result<Student> findStudentById(Integer studentId) {
        studentRepository.findById(studentId);
        return new Result<Student>(
                Result.ResultStatus.SUCCESS.status,
                "find success",studentRepository.findById(studentId).get()
        );
    }

    //分页查找
    @Override
    public Page<Student> getStudentsBySearchVo(SearchVo searchVo) {
        //1.准备分页类Pageable的数据sort，page，total
        Sort.Direction direction = "desc".equalsIgnoreCase(searchVo.getSort())?
                                          Sort.Direction.DESC:Sort.Direction.ASC;
        Sort sort = Sort.by(direction, StringUtils.isBlank(searchVo.getOrderBy())?
                            "studentId" : searchVo.getOrderBy());
        Pageable pageable = PageRequest.of(searchVo.getCurrentPage()-1,searchVo.getPageSize(),sort);
        /*
        Student student = new Student();
        student.setStudentName(searchVo.getKeyWord());
        ExampleMatcher matcher = ExampleMatcher.matching()


                .withIgnorePaths("studentId");
        Example<Student> example = Example.of(student, matcher);*/

        //2.准备findall需要的数据pageable、example
        // build Example 对象
        // 如果 keyWord 为 null，则设置的 studentName 不参与查询条件
        Student s = new Student();
        s.setStudentName(searchVo.getKeyWord());
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                // 全部模糊查询，即 %{studentName} %
                //.withMatcher("studentName", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("studentName",match -> match.contains())
                // 忽略字段，即不管id是什么值都不加入查询条件
                .withIgnorePaths("studentId");
        Example<Student> example = Example.of(s, exampleMatcher);
        return studentRepository.findAll(example,pageable);

    }

    @Override
    public List<Student> getStudents() {
        Sort.Direction direction = Sort.Direction.DESC;
      //  Sort sort = new Sort(direction, "studentName");
        Sort sort = Sort.by(direction, "studentName");
        return studentRepository.findAll(sort);
    }

    @Override
    public List<Student> getStudentsByStudentName(String studentName, int cardId) {
        if (cardId > 0) {
            return studentRepository.getStudentsByParams(studentName, cardId);
        } else {
//        return Optional
//                .ofNullable(studentRepository.findByStudentName(studentName))
//                .orElse(Collections.emptyList());
//        return Optional
//                .ofNullable(studentRepository.findByStudentNameLike(
//                        String.format("%s%S%s", "%", studentName, "%")))
//                .orElse(Collections.emptyList());
            return Optional
                    .ofNullable(studentRepository.findTop2ByStudentNameLike(
                            String.format("%s%S%s", "%", studentName, "%")))
                    .orElse(Collections.emptyList());
        }
    }
}
